﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Rhino.Mocks;
using StrategicDataSystems.Cloud.Monitoring.HostedServices;
using StrategicDataSystems.Cloud.Monitoring.UI.ViewModels;
using StrategicDataSystems.Cloud.Monitoring.Configuration;
using StrategicDataSystems.Cloud.Monitoring.UI.Services;
using StrategicDataSystems.Cloud.Monitoring.UI;

namespace StrategicDataSystems.Cloud.Monitoring.Tests.Given_A_SysTrayViewModel
{
    [TestFixture]
    public class When_Created
    {

        [Test]
        public void Should_Retrieve_HostedServices_From_Gateway()
        {
            var mockHostedServiceGateway = MockRepository.GenerateMock<IHostedServiceManagement>();
            var stubNotificationService = MockRepository.GenerateStub<INotificationService>();
            var mockTimerService = MockRepository.GenerateMock<ITimerService>();
            var stubNetPresenceService = MockRepository.GenerateStub<INetPresenceService>();
            var fakeHostedServices = new List<HostedService>();
            Settings settings = new Settings();

            mockHostedServiceGateway.Stub(
                x => x.GetHostedServicesForSubscription(settings.SubscriptionId, settings.CertificateThumbprint))
                .Return(fakeHostedServices);
            stubNetPresenceService.Stub(x => x.InternetConnectionIsAvailable()).Return(true);

            SysTrayViewModel viewModel = new SysTrayViewModel(mockHostedServiceGateway, settings, stubNotificationService, mockTimerService, stubNetPresenceService);
            
            mockHostedServiceGateway.AssertWasCalled(x => x.GetHostedServicesForSubscription(settings.SubscriptionId, settings.CertificateThumbprint));
            CollectionAssert.AreEquivalent(fakeHostedServices, viewModel.HostedServices);
        }

        [Test]
        public void Should_Start_Refresh_Timer()
        {
            var stubHostedServiceGateway = MockRepository.GenerateStub<IHostedServiceManagement>();
            var stubNotificationService = MockRepository.GenerateStub<INotificationService>();
            var stubNetPresenceService = MockRepository.GenerateStub<INetPresenceService>();
            var mockTimerService = MockRepository.GenerateMock<ITimerService>();
            var fakeHostedServices = new List<HostedService>();
            Settings settings = new Settings();

            stubHostedServiceGateway.Stub(
                x => x.GetHostedServicesForSubscription(settings.SubscriptionId, settings.CertificateThumbprint))
                .Return(fakeHostedServices);

            SysTrayViewModel viewModel = new SysTrayViewModel(stubHostedServiceGateway, settings, stubNotificationService, mockTimerService, stubNetPresenceService);

            mockTimerService.AssertWasCalled(x => x.Start(Arg<int>.Is.Anything, Arg<EventHandler>.Is.Anything));
        }

        [Test]
        public void Should_Show_No_Active_Deployments_If_Can_Not_Reach_Internet()
        {
            var stubHostedServiceGateway = MockRepository.GenerateStub<IHostedServiceManagement>();
            var stubNotificationService = MockRepository.GenerateStub<INotificationService>();
            var stubNetPresenceService = MockRepository.GenerateStub<INetPresenceService>();
            var mockTimerService = MockRepository.GenerateMock<ITimerService>();
            var fakeHostedServices = new List<HostedService>();
            Settings settings = new Settings();

            
            stubNetPresenceService.Stub(x => x.InternetConnectionIsAvailable()).Return(false);

            SysTrayViewModel viewModel = new SysTrayViewModel(stubHostedServiceGateway, settings, stubNotificationService, mockTimerService, stubNetPresenceService);

            Assert.AreEqual(0, viewModel.ActiveDeployments.Count());
        }

        [Test]
        public void Should_Not_Attempt_Hosted_Service_Call_If_No_Internet_Connection_Is_Present()
        {
            var stubHostedServiceGateway = MockRepository.GenerateStub<IHostedServiceManagement>();
            var stubNotificationService = MockRepository.GenerateStub<INotificationService>();
            var stubTimerService = MockRepository.GenerateStub<ITimerService>();
            var stubNetPresenceService = MockRepository.GenerateStub<INetPresenceService>();
            var fakeHostedServices = new List<HostedService>();
            Settings settings = new Settings();

            stubNetPresenceService.Stub(x => x.InternetConnectionIsAvailable()).Return(false);

            SysTrayViewModel viewModel = new SysTrayViewModel(stubHostedServiceGateway, settings, stubNotificationService, stubTimerService, stubNetPresenceService);
        
            stubHostedServiceGateway.AssertWasNotCalled(x => x.GetHostedServicesForSubscription(settings.SubscriptionId, settings.CertificateThumbprint));
        }

        [Test]
        public void Should_Set_Status_To_Disconnected_If_No_Internet_Connection_Is_Present()
        {
            var stubHostedServiceGateway = MockRepository.GenerateStub<IHostedServiceManagement>();
            var stubNotificationService = MockRepository.GenerateStub<INotificationService>();
            var stubTimerService = MockRepository.GenerateStub<ITimerService>();
            var stubNetPresenceService = MockRepository.GenerateStub<INetPresenceService>();
            var fakeHostedServices = new List<HostedService>();
            Settings settings = new Settings();

            stubNetPresenceService.Stub(x => x.InternetConnectionIsAvailable()).Return(false);

            SysTrayViewModel viewModel = new SysTrayViewModel(stubHostedServiceGateway, settings, stubNotificationService, stubTimerService, stubNetPresenceService);

            Assert.IsFalse(viewModel.InternetConnectionIsAvailable);
        }

        [Test]
        public void Should_Set_Status_To_Connected_If_Internet_Connection_Is_Present()
        {
            var stubHostedServiceGateway = MockRepository.GenerateStub<IHostedServiceManagement>();
            var stubNotificationService = MockRepository.GenerateStub<INotificationService>();
            var stubTimerService = MockRepository.GenerateStub<ITimerService>();
            var stubNetPresenceService = MockRepository.GenerateStub<INetPresenceService>();
            var fakeHostedServices = new List<HostedService>();
            Settings settings = new Settings();

            stubHostedServiceGateway.Stub(
                x => x.GetHostedServicesForSubscription(settings.SubscriptionId, settings.CertificateThumbprint))
                .Return(fakeHostedServices);
            stubNetPresenceService.Stub(x => x.InternetConnectionIsAvailable()).Return(true);

            SysTrayViewModel viewModel = new SysTrayViewModel(stubHostedServiceGateway, settings, stubNotificationService, stubTimerService, stubNetPresenceService);

            Assert.IsTrue(viewModel.InternetConnectionIsAvailable);
        }
    }

}